package dvx.mp.ivisual.desktop.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;

import org.apache.log4j.Logger;

import dvx.mp.ivisual.desktop.domain.invoices.InvoiceService;
import dvx.mp.ivisual.desktop.ui.model.GridImportInvoicesModel;
import dvx.mp.ivisual.desktop.ui.util.FileExtension;
import dvx.mp.ivisual.desktop.ui.util.GridCalendarRenderer;
import dvx.mp.ivisual.desktop.ui.util.GridNumberRenderer;

public class ImportarFacturas extends JInternalFrame {
	/***/
	private static final long serialVersionUID = 1L;
	private final Logger log = Logger.getLogger(ImportarFacturas.class);
	
	
	private JTable grdFacturas;
	private JButton btnQuitarSeleccionadas;
	private JButton btnExaminar;

	/**
	 * Create the frame.
	 */
	public ImportarFacturas() {
		super("Importación de facturas");
		setResizable(true);
		setMaximizable(true);
		setClosable(true);
		setBounds(100, 100, 450, 300);

		this.createUI();
		this.bindEvents();
	}

	private void createUI()	{
		JPanel pnlBotones = new JPanel();
		getContentPane().add(pnlBotones, BorderLayout.NORTH);
		pnlBotones.setLayout(new FlowLayout(FlowLayout.LEADING, 5, 5));
		
		btnExaminar = new JButton("Examinar...");
		btnExaminar.setHorizontalAlignment(SwingConstants.LEFT);
		pnlBotones.add(btnExaminar);
		
		JLabel label = new JLabel("::::");
		pnlBotones.add(label);
		
		btnQuitarSeleccionadas = new JButton("Quitar seleccionadas");
		pnlBotones.add(btnQuitarSeleccionadas);
		
		JPanel pnlGrid = new JPanel();
		getContentPane().add(pnlGrid, BorderLayout.CENTER);
		pnlGrid.setLayout(new GridLayout(1, 0, 0, 0));
		
		grdFacturas = new JTable();
		grdFacturas.setDefaultRenderer(Calendar.class, new GridCalendarRenderer());
		grdFacturas.setDefaultRenderer(Double.class, GridNumberRenderer.getCurrencyRenderer());
		
		pnlGrid.add(new JScrollPane(grdFacturas));
		
		JLabel lblConsejo = new JLabel("Arrastre los archivos XML de facturas hacia la lista o de clic en el botón examinar para comenzar la importación.");
		getContentPane().add(lblConsejo, BorderLayout.SOUTH);
	}
	
	private void bindEvents()	{
		btnExaminar.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg) {
				JFileChooser fileChooser = new JFileChooser();
				fileChooser.setAcceptAllFileFilterUsed(false);
				fileChooser.setFileFilter(new FileExtension(FileExtension.XML));
				fileChooser.setMultiSelectionEnabled(true);
				
				int response = fileChooser.showOpenDialog(ImportarFacturas.this);
				
				if(JFileChooser.APPROVE_OPTION != response)
				{ return; }
				
				InvoiceService iservice = InvoiceService.getInstance();
				
				GridImportInvoicesModel grdModel = new GridImportInvoicesModel();
				grdModel.setInvoices(iservice.importInvoicesFromFiles(fileChooser.getSelectedFiles()));
				
				ImportarFacturas.this.grdFacturas.setModel(grdModel);
				
				log.debug("Grid Model Rowcount: " + grdModel.getRowCount());
			}
		});
	}
}
